package controller

import (
	"changeme/Z"
	"changeme/module"
	"fmt"
	"github.com/guonaihong/gout"
	"gopkg.in/yaml.v2"
	"io/ioutil"
)

var Url string
var Token string
var AllPages string

func GetBlogPosts(url string) Z.H {
	var blogPostsModel module.BlogPostsModel
	var blogHeaderModel module.BlogHeaderModel
	api := url + "/wp-json/wp/v2/posts"
	page := "1"
	err := gout.GET(api).SetJSON(gout.H{"page": page}).BindJSON(&blogPostsModel).BindHeader(&blogHeaderModel).Do()
	if err != nil {
		return Z.H{
			"code": 400,
			"msg":  err,
		}
	}
	AllPages = blogHeaderModel.XWpTotalpages
	return Z.H{
		"code":  200,
		"total": blogHeaderModel.XWpTotal,
		"pages": page,
		"msg":   blogPostsModel,
	}
}

func PostPages(url string, page string) Z.H {
	var blogPostsModel module.BlogPostsModel
	var blogHeaderModel module.BlogHeaderModel
	api := url + "/wp-json/wp/v2/posts"
	err := gout.GET(api).SetJSON(gout.H{"page": page}).BindJSON(&blogPostsModel).BindHeader(&blogHeaderModel).Do()
	if err != nil {
		return Z.H{
			"code": 400,
			"msg":  err,
		}
	}
	AllPages = blogHeaderModel.XWpTotalpages
	return Z.H{
		"code":  200,
		"total": blogHeaderModel.XWpTotal,
		"pages": page,
		"msg":   blogPostsModel,
	}

}

func LoginGetToken(username string, password string, url string) bool {
	var getTokenModel module.GetTokenModel
	Url = url
	api := url + "/wp-json/jwt-auth/v1/token"
	gout.POST(api).SetJSON(gout.H{"username": username, "password": password}).BindJSON(&getTokenModel).Do()
	if getTokenModel.Token == "" {
		return false
	} else {
		Token = getTokenModel.Token
		return true
	}
}

func Login(name string, pass string, url string) {
	conFilePath := "./config/init.yaml"
	var initYamlConModel module.InitYamlConModel
	file, _ := ioutil.ReadFile(conFilePath)
	_ = yaml.Unmarshal(file, &initYamlConModel)
	initYamlConModel.Init.Username = name
	initYamlConModel.Init.Password = pass
	initYamlConModel.Init.WebUrl = url
	marshal, err := yaml.Marshal(&initYamlConModel)
	if err != nil {
		fmt.Println(err)
	}
	err = ioutil.WriteFile(conFilePath, marshal, 0777)
	if err != nil {
		fmt.Println(err)
	}
}

func PutBlog(key string, title string, content string) Z.H {
	var resBind map[string]interface{}
	fmt.Println("id is:", key)
	api := Url + "/wp-json/wp/v2/posts/" + key
	err := gout.PUT(api).
		SetJSON(gout.H{"title": title, "content": content}).
		SetHeader(gout.H{"authorization": "Bearer " + Token}).
		BindJSON(&resBind).Do()
	fmt.Println("res:", resBind)
	if err != nil {
		return resBind
	}
	return resBind
}

func ReleaseBlog(title string, content string) map[string]interface{} {
	var bindJson map[string]interface{}
	api := Url + "/wp-json/wp/v2/posts"
	gout.POST(api).SetJSON(gout.H{"title": title, "content": content, "status": "publish"}).
		SetHeader(gout.H{"authorization": "Bearer " + Token}).
		BindJSON(&bindJson).
		Do()
	return bindJson
}

func DeleteArticle(id string) map[string]interface{} {
	var bindJson map[string]interface{}
	api := Url + "/wp-json/wp/v2/posts/" + id
	gout.DELETE(api).SetJSON(gout.H{"force": false}).
		SetHeader(gout.H{"authorization": "Bearer " + Token}).
		BindJSON(&bindJson).Do()
	return bindJson
}
